package com.feng.zshop.backend.controller;

import com.feng.zshop.backend.service.ProductTypeService;
import com.feng.zshop.common.constant.PaginationConstant;
import com.feng.zshop.common.exception.ProductTypeExistsException;
import com.feng.zshop.common.utils.ResponseResult;
import com.feng.zshop.model.ProductType;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/backend/productType")
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;

    @GetMapping("/findAll")
    public String findAllProductType(@RequestParam(value = "pageNum", required = false, defaultValue = "1")
                                             Integer pageNum, Model model) {
        Page<ProductType> page = PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
        List<ProductType> productTypes = productTypeService.findAllProductType();

        PageInfo<ProductType> pageInfo = new PageInfo<>(productTypes);
        model.addAttribute("pageInfo", pageInfo);
        return "productTypeManager";
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseResult addProductType(@RequestParam("productType") String productType) {
        try {
            productTypeService.addProductType(productType);
            return ResponseResult.successNoData("产品添加成功");
        } catch (ProductTypeExistsException e) {
            return ResponseResult.failNoData("产品名称已经存在");
        }
    }

    @GetMapping("/removeById")
    @ResponseBody
    public ResponseResult removeProductTypeById(@RequestParam(value = "id", required = true) Integer id) {
        productTypeService.removeProductTypeById(id);
        return ResponseResult.successNotMessageAndData();
    }

    @PostMapping("/findById")
    @ResponseBody
    public ResponseResult findById(@RequestParam(value = "id", required = true) Integer id){
        ProductType productType = productTypeService.selectProductTypeById(id);
        return ResponseResult.successNoMessage(productType);
    }

    @PostMapping("/modifyName")
    @ResponseBody
    public ResponseResult modifyName(@RequestParam(value = "id", required = true) Integer id,@RequestParam(value = "name", required = true) String name){
      try {
          productTypeService.updateProductName(id,name);
          return ResponseResult.successNoData("修改成功");
      } catch (ProductTypeExistsException e){
          return ResponseResult.failNoData("商品名称已经存在");
      }
    }

    @GetMapping("/modifyStatus")
    @ResponseBody
    public ResponseResult modifyStatus(@RequestParam(value = "id", required = true) Integer id){
        productTypeService.updateProductTypeStatus(id);
        return ResponseResult.successNotMessageAndData();
    }

}
